    //
//  GraphViewController.m
//  GraphingCalculator
//
//  Created by Willem van Willigen on 25/02/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GraphViewController.h"
#import "CalculatorBrain.h"


@implementation GraphViewController

@synthesize graphView;
@synthesize expression;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

	



- (void)setExpression:(id)anExpression {
	[expression release];
	expression = [anExpression retain];
	[self updateView];
}

- (float)graphValueForGraphView:(GraphView *)requestor xValue:(float)aValue {
	NSDictionary *values = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:aValue] forKey:@"x"];
	return [CalculatorBrain evaluateExpression:self.expression usingVariableValues:values];
}


- (void)setup {
	self.title = [CalculatorBrain descriptionOfExpression:self.expression];
	CGRect newFrame = self.parentViewController.view.bounds;
	NSLog(@"%@", self.parentViewController);
	newFrame.size.height = newFrame.size.height - self.navigationController.navigationBar.bounds.size.height;
	NSLog(@"setup: parent bounds: %g %g %g %g", newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height);
	graphView.frame = newFrame;
	//graphView.midPoint = [graphView defaultMidPoint];
	//[self updateView];
	NSLog(@"setup called");

}

- (void)updateView {
	[self setup];
	[self.graphView setNeedsDisplay];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	self.graphView.delegate = self;
	//[self setup];
}


- (void) loadView {
	[super loadView];
	
	CGRect frame = self.navigationController.view.bounds;
	
	//frame.size.height = frame.size.height - self.navigationController.navigationBar.bounds.size.height;
	self.graphView = [[GraphView alloc] initWithFrame:frame pinchingAllowed:YES panningAllowed:YES];
	[self.graphView setBackgroundColor:[UIColor whiteColor]];
	
	[self.view addSubview:self.graphView];
	
	
	NSLog(@"graphVC loadView");
}
	
/*- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
	NSLog(@"willRotate");
	CGRect test = graphView.bounds;
	NSLog(@"navcon bounds will: %g %g %g %g", test.origin.x, test.origin.y, test.size.width, test.size.height);
	NSLog(@"navbar height will: %g", self.navigationController.navigationBar.bounds.size.height);
	if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
		toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
		graphView.frame = CGRectMake(0, 0, 480,300);
	} else {
		graphView.frame = CGRectMake(0, 0, 320, 460);
	}
	[graphView setNeedsDisplay];
	
}*/
	
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
	[self setup];
	NSLog(@"rotate happened");
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)splitViewController:(UISplitViewController *)svc
	 willHideViewController:(UIViewController *)aViewController
		  withBarButtonItem:(UIBarButtonItem*)barButtonItem
	   forPopoverController:(UIPopoverController*)pc
{
	barButtonItem.title = @"Menu";
	self.navigationItem.leftBarButtonItem = barButtonItem;
	NSLog(@"will hide %@ with title %@", aViewController, aViewController.title);
}

- (void)splitViewController:(UISplitViewController *)svc
	 willShowViewController:(UIViewController *)aViewController
  invalidatingBarButtonItem:(UIBarButtonItem *)button
{
	NSLog(@"will show");
	self.navigationItem.leftBarButtonItem = nil;
	//self.navigationItem.backBarButtonItem = nil;
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (void)dealloc {
	[graphView release];
	[expression release];
	NSLog(@"graphViewController dealloc");
    [super dealloc];
}


@end
